import { EggAppConfig, EggAppInfo, PowerPartial } from "egg";

export default (appInfo: EggAppInfo) => {
  const config = {} as PowerPartial<EggAppConfig>;

  config.keys = appInfo.name + "_1651739919003_987";

  config.middleware = ["graphql"];

  // POST
  config.bodyParser = {
    enableTypes: ["json", "form", "text"],
    extendTypes: {
      text: ["text/xml", "application/xml"],
    },
    jsonLimit: "100mb",
  };

  config.cors = {
    allowMethods: "GET,HEAD,PUT,POST,DELETE,PATCH",
    credentials: true,
  };

  // egg安全相关
  config.security = {
    csrf: {
      enable: false,
    },
    domainWhiteList: [
      "http://localhost:8000",
      "http://localhost:8001",
      "http://192.168.0.14:8000",
      "http://192.168.0.108:8000",
      "http://192.168.0.166:8000",
      "http://192.168.0.14:8024",
    ],
  };

  config.onerror = {
    all(err, ctx) {
      ctx.logger.error("发现未知错误", err);
      ctx.body = { code: 500, msg: "未知错误 " + err };
      ctx.status = 500;
    },
  };

  const bizConfig = {
    sourceUrl: `https://github.com/eggjs/examples/tree/master/${appInfo.name}`,

    jwt: {
      // 密钥：由43位大小写字数和数字组成。
      SECRET: "yLuYYVOP0NtQyI6N03evUx7bcBF2JCmE6T47gMERR3B",
    },
  };

  return {
    ...config,
    ...bizConfig,
  };
};
